package redlib.backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redlib.backend.annotation.BackendModule;
import redlib.backend.annotation.NeedNoPrivilege;
import redlib.backend.annotation.Privilege;
import redlib.backend.dto.InstrumentDTO;
import redlib.backend.dto.query.InstrumentQueryDTO;
import redlib.backend.model.Page;
import redlib.backend.model.Statistics;
import redlib.backend.service.InstrumentService;
import redlib.backend.vo.InstrumentStatisticsVO;
import redlib.backend.vo.InstrumentVO;

import java.util.List;

/**
 * 部门管理后端服务模块
 *
 * @author 李洪文
 * @description
 * @date 2019/12/3 11:07
 */
@RestController
@RequestMapping("/api/instrument")
@BackendModule({"page:页面", "update:修改", "add:创建", "delete:删除"})

public class InstrumentController {
    @Autowired
    private InstrumentService instrumentService;

    @PostMapping("listInstrument")
    @Privilege("page")
    public Page<InstrumentVO> listInstrument(@RequestBody InstrumentQueryDTO queryDTO) {
        return instrumentService.listByPage(queryDTO);
    }

    @PostMapping("addInstrument")
    @Privilege("add")
    public Integer addInstrument(@RequestBody InstrumentDTO instrumentDTO) {
        return instrumentService.addInstrument(instrumentDTO);
    }

    @PostMapping("updateInstrument")
    @Privilege("update")
    public Integer updateInstrument(@RequestBody InstrumentDTO instrumentDTO) {
        return instrumentService.updateInstrument(instrumentDTO);
    }

    @PostMapping("deleteInstrument")
    @Privilege("delete")
    public void deleteInstrument(@RequestBody List<Integer> ids) {
        instrumentService.deleteByCodes(ids);
    }

    @GetMapping("getInstrumentByCode")
    @NeedNoPrivilege
    public String getInstrumentByCode() {
        return "Hello,world";
    }

    @PostMapping("Statistics")
    public Statistics<InstrumentStatisticsVO> statistics(){
        return instrumentService.statistics();
    }


    @PostMapping("repairByCodes")
    @Privilege("update")
    public String repairByCodes(@RequestBody List<Integer> ids) {
        instrumentService.repairByCodes(ids);
        return "报修成功";
    }

    @PostMapping("repairedByCodes")
    @Privilege("update")
    public String repairedByCodes(@RequestBody List<Integer> ids) {
        instrumentService.repairedByCodes(ids);
        return "维修成功";
    }
}
